﻿#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
#include<cstdio>
#include<Windows.h>
using namespace std;

struct Merchant {
	string name;
	string location;
	float totalScore;
};


int land(string filename)//登录操作
{
	while (1)
	{
		cout << "      -              请输入您的账号：             -" << endl;
		string client_number;
		cin >> client_number;
		//从账号密码文件（3个文档）中读取搜索读取账号和密码；	
		ifstream file(filename);
		string f;
		if (file.is_open())
		{
			string client1_number;
			bool mark = 0;
			bool mark2 = 0;
			while (getline(file, client1_number))
			{
				if (client_number == client1_number)//格式  账号（回车）密码
				{
					mark2 = 1;
					cout << "      -              请输入您的密码：             -" << endl;
					string password_stdin;
					cin >> password_stdin;
					string password_client1;
					getline(file, password_client1);
					if (password_stdin == password_client1)
					{
						cout << "                   -登入成功！-                 " << endl;
						int value_return = 2;
						return value_return;
					}
					else
					{
						cout << "             -密码错误！请重新输入！-                 " << endl;
						Sleep(2000);
						cout << "\033[5A";
						cout << "\033[J";
						mark = 1;
						break;
					}
				}
			}
			//cout << "hhhhhhhhhhhhh";
			if (mark2 == 0)
			{
				cout << "账户号错误！请重新输入！" << endl;
				continue;
			}
			file.close();
			if (mark == 1)
				continue;
		}
		else//比较
		{
			cout << "错误！请先注册！" << endl;
			Sleep(1500);
			system("cls");
			return 0;
		}
	}
}

int client_register(string filename)//注册操作
{
	ifstream file(filename);
	if (filename == "client1.txt")//验证管理员密钥
	{
		ifstream file1("admin.txt");
		if (file1.is_open())//验证管理员密钥
		{

			string admin_key;
			getline(file1, admin_key);
			file1.close();
			cout << "      -              请输入管理员密钥：           -" << endl;
			string admin_stdin;
			cin >> admin_stdin;
			if (admin_key != admin_stdin)
			{
				cout << "警告！请重新操作！";
				return -1;
			}
		}
		else//创建文档存储账号和密码，创建文档存储管理员密钥
		{
			ofstream file_admin("admin.txt", ios::app);
			cout << "      -              请设置管理员密钥：           -" << endl;
			string admin_key;
			cin >> admin_key;
			file_admin << admin_key << endl;
			file_admin.close();
		}
	}
set_client:
	cout << "      -              请设置您的账户号码：         -" << endl;
	cout << "      -   例子：（输入）000，则您的账户号码为000  -" << endl;
	string client_number;
	cin >> client_number;
	string client_password;
	cout << "      -              请设置您的账户密码：         -" << endl;
	cin >> client_password;
	string client1_number;
	while (getline(file, client1_number))
	{
		if (client_number == client1_number)//格式  账号（回车）密码
		{
			cout << "      -             账号重复！请重新输入！           -" << endl;
			Sleep(2000);
			cout << "\033[4A";
			cout << "\033[J";
			goto set_client;
		}
	}
	file.close();
	ofstream file2(filename, ios::app);
	file2 << client_number << std::endl;
	file2 << client_password << std::endl;
	file2.close();
	cout << "      -             注册成功！           -" << endl;
	return 0;
}

int end()//退出系统
{
	cout << "      -                  请选择：                 -" << endl;
	cout << "      -               返回登录请扣1               -" << endl;
	cout << "      -            退出系统请扣任意数字           -" << endl;
	int value_operate;
	cin >> value_operate;
	if (value_operate == 1)
		return 5;
	else
		return 0;
}

int password_change(string filename, int n, int kk)//修改密码&查看密码
{
change_start:
	if (n == 1)
		cout << "      -       请输入您需要修改密码的账户号：      -" << endl;
	else
		cout << "      -       请输入您需要查找密码的账户号：      -" << endl;
	string client_change;
	cin >> client_change;
	ifstream file(filename);
	string client_file;
	string client_password;
	bool mark = 0;
	while (getline(file, client_file))
	{
		getline(file, client_password);
		if (client_change == client_file)
		{
			mark = 1;
			break;
		}
	}
	file.close();
	if (n != 1)
	{
		cout << "账号" << client_change << "的密码是：    ";
		cout << client_password << endl;
	}
	else
	{
		if (mark == 0)//账户不存在
		{
			cout << "      -                请重新输入账号            -" << endl;
			Sleep(2000);
			cout << "\033[3A";
			cout << "\033[J";
			goto change_start;
		}
		fstream file_change(filename, ios::in | ios::out | ios::app);
		ofstream password_change("Transcipt0.txt", ios::app);
		cout << "      -                请输入更改后的密码：          -" << endl;
		string passwordid_change;
		cin >> passwordid_change;
		string merchant_filename;	//读取的每一行文字
		while (getline(file_change, merchant_filename))
		{
			if (merchant_filename == client_change)
			{
				file_change << client_change << std::endl;
				file_change << passwordid_change << std::endl;
				getline(file_change, merchant_filename);
			}
			else
				password_change << merchant_filename << std::endl;
		}
		password_change.close();
		file_change.close();
		switch (kk)
		{
		case 1:
		{
			if (remove("client1.txt") == 0)
			{
				if (rename("Transcript0.txt", "client1.txt"))
					cout << "      -                    修改成功！                -" << endl;
				else
					cerr << "文件重命名失败。 " << "管理员账号密码副本" << endl;
			}
			else
				cerr << "文件删除失败。 " << "管理员账号密码原文档" << endl;
			break;
		}
		case 2:
		{
			if (remove("client2.txt") == 0)
			{
				if (rename("Transcript0.txt", "client2.txt"))
					cout << "      -                    修改成功！                -" << endl;
				else
					cerr << "文件重命名失败。 " << "学生账号密码副本" << endl;
			}
			else
				cerr << "文件删除失败。 " << "学生账号密码原文档" << endl;
			break;
		}
		case 3:
		{
			if (remove("client3.txt") == 0)
			{
				if (rename("Transcript0.txt", "client3.txt"))
					cout << "      -                    修改成功！                -" << endl;
				else
					cerr << "文件重命名失败。 " << "食堂人员账号密码副本" << endl;
			}
			else
				cerr << "文件删除失败。 " << "食堂人员账号密码原文档" << endl;
		}
		}
	}
	cout << "      -             请选择：返回登录请扣1            -" << endl;
	cout << "      -              退出系统请扣任意数字            -" << endl;
	int num;
	cin >> num;
	return num;
}

int client_master()//账户管理
{
	cout << "      -                  请选择：                 -" << endl;
	cout << "      -               修改密码请扣1               -" << endl;
	cout << "      -            查看账户信息请扣任意数字       -" << endl;
	int value_cin;
	cin >> value_cin;
	if (value_cin == 1)
		cout << "      -       请选择您要修改的密码的账户类型：    -" << endl;
	else
		cout << "      -       请选择您要查看的密码的账户类型：    -" << endl;
	cout << "      -               管理员请扣1                 -" << endl;
	cout << "      -                 学生请扣2                 -" << endl;
	cout << "      -         食堂相关人员请扣任意数字          -" << endl;
	int next_cin;
	cin >> next_cin;
	if (next_cin == 1)
	{
		string filename = "client1.txt";
		int num = password_change(filename, value_cin, 1);
		return num;
	}
	else if (next_cin == 2)
	{
		string filename = "client2.txt";
		int num = password_change(filename, value_cin, 2);
		return num;
	}
	else
	{
		string filename = "client3.txt";
		int num = password_change(filename, value_cin, 3);
		return num;
	}
}

int merchant_output()//查看商户评价（没有输出商户列表）
{
	ifstream file("merchant_message.txt");
start:
	cout << "      -         请输入要查看信息的商户名字        -" << endl;
	string merchant_evaluation, merchant_name;
	cin >> merchant_evaluation;
	while (getline(file, merchant_name))
	{
		if (merchant_name == merchant_evaluation)
		{
			while (getline(file, merchant_name))
			{
				if (merchant_name == "-END-")
					break;
				if (merchant_name == "TOTAL SCORE:")
				{
					cout << "总评分：" << endl;
					continue;
				}
				if (merchant_name == "SINGLE SCORE:")
				{
					cout << "单个评分：" << endl;
					continue;
				}
				cout << merchant_name << endl;
			}
		}
		else
		{
			cout << "名字错误！请重新输入！" << endl;
			goto start;
		}
	}
	file.close();
	return 0;
}

int merchant_message()//商户信息管理
{
	//增加、删除
	//用一个文件存储所有的商户信息
	cout << "      -                  请选择：                 -" << endl;
	cout << "      -               查看信息请扣1               -" << endl;
	cout << "      -     添加、删除、修改商户信息请扣任意数字  -" << endl;
	int num;
	cin >> num;
	if (num == 1)//查看商户信息
	{
		merchant_output();
		cout << endl;
		cout << endl;
		cout << endl;
		cout << "      -             请选择：返回登录请扣1            -" << endl;
		cout << "      -              退出系统请扣任意数字            -" << endl;
		int numm;
		cin >> numm;
		return numm;
	}
	else//增加、删除、修改信息
	{
	add_start:
		cout << "      -                  请选择：                 -" << endl;
		cout << "      -               增添新商户请扣1             -" << endl;
		cout << "      -              删除商户信息请扣2            -" << endl;
		cout << "      -           退出商户信息管理系统请扣3       -" << endl;
		cout << "      -         修改商户信息请扣其他任意数字      -" << endl;
		cin >> num;
		if (num == 1)//增添新商户
		{
			ofstream file_change("merchant_message.txt", ios::app);
			while (1)
			{
				string merchant_message_input;
				cout << "      -                请输入商户名：             -" << endl;
				cin >> merchant_message_input;
				file_change << merchant_message_input << std::endl;
				cout << "      -               请输入商户位置：            -" << endl;
				cin >> merchant_message_input;
				file_change << merchant_message_input << std::endl;
				file_change << "TOTAL SCORE:" << std::endl;
				file_change << "SINGLE SCORE:" << std::endl;
				file_change << "-END-" << std::endl;
				cout << "      -                  请选择：                 -" << endl;
				cout << "      -              继续增添商户请扣1            -" << endl;
				cout << "      -              返回上一界面请扣2            -" << endl;
				cin >> num;
				if (num == 2)
					break;
			}
			file_change.close();
			goto add_start;
		}
		else if (num != 3)//删除或修改商户信息
		{
			fstream file_change("merchant_message.txt", ios::in | ios::out | ios::app);
			ofstream file_delete("Transcript.txt", ios::app);
			string merchant_name;
			string merchant_filename;	//读取的每一行文字
			if (num == 2)//删除信息
				cout << "      -            请输入要删除的商户名字         -" << endl;
			else		//修改信息
				cout << "      -            请输入要修改的商户名字         -" << endl;
			cin >> merchant_name;
			while (getline(file_change, merchant_filename))
			{
				if (merchant_filename == merchant_name)
				{
					if (num == 2)//删除信息
					{
						while (getline(file_change, merchant_filename))
						{
							if (merchant_filename == "-END-")
								break;
						}
						//恰好删掉了商家信息
					}
					else		//修改信息
					{
						string merchant_message_input;
						cout << "      -          请输入修改过后所有的商户信息     -" << endl;
						cout << "      -   *仅支持修改基础信息，暂不允许修改评价   -" << endl;
						cout << endl;
						cout << "      -                请输入商户名：             -" << endl;
						cin >> merchant_message_input;
						file_change << merchant_message_input << std::endl;
						cout << "      -               请输入商户位置：            -" << endl;
						cin >> merchant_message_input;
						file_change << merchant_message_input << std::endl;
						//把评价复制到副本
						while (getline(file_change, merchant_filename))
						{
							if (merchant_filename == "-END-")
								break;
						}
					}
				}
				else//没有读到那一行的信息，将信息复写到另一个文档
				{
					file_delete << merchant_filename;
					file_delete << endl;
				}
			}
			file_change.close();
			file_delete.close();
			//重命名并删掉副本
			if (remove("merchant_message.txt") == 0)
			{
				if (rename("Transcript.txt", "merchant_message.txt") == 0)
				{
					cout << endl;
					cout << endl;
					cout << endl;
					cout << "      -             请选择：返回登录请扣1            -" << endl;
					cout << "      -              退出系统请扣任意数字            -" << endl;
					int numm;
					cin >> numm;
					return numm;
				}
				else
					cerr << "文件重命名失败。 " << "商户信息文档副本" << endl;
			}
			else
				cerr << "文件删除失败。 " << "商户信息文档" << endl;
		}
		else if (num == 3)
		{
			cout << endl;
			cout << endl;
			cout << endl;
			cout << "      -             请选择：返回登录请扣1            -" << endl;
			cout << "      -              退出系统请扣任意数字            -" << endl;
			int numm;
			cin >> numm;
			return numm;
		}
	}
}

int admin_system()//管理员系统
{
	cout << "      -                  请选择：                 -" << endl;
	cout << "      -               账户管理请扣1               -" << endl;
	cout << "      -            商户信息管理请扣任意数字       -" << endl;
	int value_cin;
	cin >> value_cin;
	if (value_cin == 1)//账户管理
	{
		int num = client_master();
		return num;//在主函数(int main)中判断该返回值
		//返回登录=1
		//退出整个系统=任意数字
	}
	else//商户信息管理
	{
		int num = merchant_message();
		return num;
		//返回登录=1
		//退出整个系统=任意数字
	}
}

int others_system(int n)//学生和食堂人员管理系统
{
	//输出商户列表
	ifstream file_print("merchant_message.txt", ios::in);
	string merchant_name;
	int i = 0;
	while (getline(file_print, merchant_name))
	{
		if (merchant_name == "TOTAL SCORE:")
		{
			while (getline(file_print, merchant_name))
			{
				if (merchant_name == "-END-")
					break;
			}
		}
		else
		{
			if (i % 2 == 0)
				cout << merchant_name << "    ";
			else
				cout << merchant_name << endl;
			i++;
		}
	}
	//学生需要传个参数证明是学生
	//0=食堂，1=学生
	if (n == 0)
		merchant_output();
	else
	{
		cout << "      -                  请选择：                 -" << endl;
		cout << "      -             给餐厅打分评价请扣1           -" << endl;
		cout << "      -        查看餐厅的评分评价请扣任意数字     -" << endl;
		int num;
		cin >> num;
		if (num == 1)
		{
			fstream file_evaluate("merchant_message.txt", ios::in | ios::out);
			ofstream file_transcript("Transcript2.txt", ios::app);
			string merchant_name;
			string file_merchantname;
			cout << "      -          请输入需要评分的餐厅名字：       -" << endl;
			cin >> file_merchantname;
			float evaluation;
			string long_evaluation;
			cout << "      -               请输入您的评分：            -" << endl;
			cin >> evaluation;
			cout << "      -               请输入您的评价：            -" << endl;
			cin >> long_evaluation;
			while (getline(file_evaluate, merchant_name))
			{
				if (merchant_name == file_merchantname)
				{
					file_evaluate >> merchant_name;//把地址那一行输出到副本中
					file_transcript << merchant_name;
					file_transcript << endl;
					file_evaluate >> merchant_name;//TOTAL SCORE那一行
					file_transcript << merchant_name;
					file_transcript << endl;
					float total_score;//修改总评分
					file_evaluate >> total_score;
					total_score = (total_score + evaluation) / 2;
					file_transcript << total_score;
					file_transcript << endl;
					file_evaluate >> merchant_name;//把single score那一行输出到副本中
					file_transcript << merchant_name;
					file_transcript << endl;
					file_transcript << evaluation;//单个评分归档
					file_transcript << endl;
					file_transcript << long_evaluation;//单个评价归档
					file_transcript << endl;
				}
				else
				{
					file_transcript << merchant_name;//复写信息到副本中
					file_transcript << endl;
				}
			}
			// 先确保所有文件都乖巧地关闭了
			file_evaluate.close();
			file_transcript.close();

			// 给系统一点反应时间（像等奶茶冷却一样重要）
			Sleep(100);  // 需要 #include <windows.h>

			// 用最安全的方式替换文件
			const char* temp_file = "Transcript2.txt";
			const char* orig_file = "merchant_message.txt";

			// 先尝试温柔地删除原文件
			if (remove(orig_file) != 0) {
				// 如果第一次没成功...
				cout << "正在尝试获得文件权限..." << endl;

				// 轻轻地请求管理员权限
				if (system(("icacls \"" + string(orig_file) + "\" /grant Everyone:F /T >nul 2>&1").c_str()) == 0) {
					Sleep(100);  // 再等一会儿
					if (remove(orig_file) != 0) {
						cerr << "还是无法删除原文件 QAQ" << endl;
						return -1;
					}
				}
			}

			// 确保新文件已经写好
			ifstream temp(temp_file, ios::binary);
			if (!temp) {
				cerr << "临时文件不见了 (＞﹏＜)" << endl;
				return -1;
			}

			// 稳稳地重命名
			if (rename(temp_file, orig_file) != 0) {
				cerr << "重命名失败啦，但别担心~" << endl;
				cerr << "请手动把 [" << temp_file << "] 改名为 [" << orig_file << "]" << endl;
			}
			else {
				cout << "操作成功完成~ (ﾉ◕ヮ◕)ﾉ*:･ﾟ✧" << endl;
			}

			



		}
		else
			merchant_output();
	}
	return 0;
}








int main()
{
	while (1)
	{
	start:
		cout << endl << endl << endl;
		cout << "      ---------------------------------------------" << endl;
		cout << "      -      欢迎使用食堂商户信息管理系统         -" << endl;
		cout << "      -                 请选择：                  -" << endl;
		cout << "      -                登录请扣1                  -" << endl;
		cout << "      -                注册请扣2                  -" << endl;
		cout << "      ---------------------------------------------" << endl;
		cout << endl << endl << endl;
		int number1;
		cin >> number1;
		if (number1 != 1 && number1 != 2)
		{
			cout << "错误！请输入正确的序号进行下一步操作！";
			Sleep(2000);
			system("cls");
			goto start;
		}
		else
		{
		start2:
			cout << "      -              请选择您的身份：             -" << endl;
			cout << "      -               管理员请扣1                 -" << endl;
			cout << "      -                 学生请扣2                 -" << endl;
			cout << "      -            食堂相关人员请扣3              -" << endl;
		}
		int number2;
		cin >> number2;
		if (number2 != 1 && number2 != 2 && number2 != 3)
		{
			cout << "错误！请输入正确的序号进行下一步操作！";
			Sleep(2000);
			system("cls");
			goto start;
		}
		if (number1 == 1)//登录
		{
			string text_client;
			switch (number2)
			{
			case 1:
			{
				text_client = "client1.txt";
				int caozuo = land(text_client);
				if (caozuo == 0)//未成功打开文件，返回注册
					goto start;
				if (caozuo == 2)//正确操作,进入管理员系统
				{
					int mark = admin_system();
					//返回登录=1
					//退出整个系统=任意数字
					if (mark == 1)
						goto start;
				}
				break;
			}
			case 2:
			{
				text_client = "client2.txt";
				int caozuo = land(text_client);
				if (caozuo == 0)//未成功打开文件，返回注册
					goto start;
				if (caozuo == 2)//正确操作,进入学生系统
					others_system(1);
				break;
			}
			case 3:
			{
				text_client = "client3.txt";
				int caozuo = land(text_client);
				if (caozuo == 0)//未成功打开文件，返回注册
					goto start;
				if (caozuo == 2)//正确操作,进入食堂相关人员系统
					others_system(0);
			}
			}
		}
		else//注册
		{
			string text_client;
			switch (number2)
			{
			case 1:
			{
				//如果管理员文档未创建，则无需管理员密钥
				//若已创建，则需要输入管理员密钥(共用）
				text_client = "client1.txt";
				int caozuo = client_register(text_client);
				switch (caozuo)
				{
				case -1:
				{
					goto start2;
					break;
				}
				case 0:
				{
					int return_end = end();
					if (return_end == 5)
						goto start;
					else
						break;
				}
				}
			}
			case 2:
			{
				text_client = "client2.txt";
				client_register(text_client);
				int return_end = end();
				if (return_end == 5)
					goto start;
				else
					break;
			}
			case 3:
			{
				text_client = "client3.txt";
				client_register(text_client);
				int return_end = end();
				if (return_end == 5)
					goto start;
				else
					break;
			}
			}
		}
	}
	return 0;
}